﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System.Data;
using System.Diagnostics;
using System.Data.Objects;
using System.Data.EntityClient;
using System.IO;
using FR.EFVisualizer;

[assembly: DebuggerVisualizer(typeof(EFVisualizer), typeof(EFVisualizerObjectSource), Target = typeof(ObjectQuery), Description = "Entity SQL Visualizer")]
[assembly: DebuggerVisualizer(typeof(EFVisualizer), typeof(EFVisualizerObjectSource), Target = typeof(EntityCommand), Description = "Entity SQL Visualizer")]
[assembly: DebuggerVisualizer(typeof(EFVisualizer), typeof(EFVisualizerObjectSource), Target = typeof(ObjectContext), Description = "Entity SQL Visualizer")]

namespace FR.EFVisualizer
{
    public class EFVisualizerObjectSource : VisualizerObjectSource
    {
        public override void GetData(object target, System.IO.Stream outgoingData)
        {
            string sql = ((EntityCommand)target).ToTraceString();
            var sw = new StreamWriter(outgoingData);
            sw.Write(sql);
            sw.Flush();
        }
    }

    public class EFVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            var entity = objectProvider.GetObject();
        }

        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(System.Data.Objects.ObjectQuery));
            visualizerHost.ShowVisualizer();
        }
    }
}
